<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定时器的任务轮询</title>
    <style>
        div{
            width: 100px;
            height: 100px;
            background: greenyellow;
            position: relative;
        }
    </style>
</head>
<body>
    <div></div>
    <script>
        function interval(callback,delay=50){
            let id = setInterval(() => callback(id),delay);
        }
        interval(timeId => {
            const div = document.querySelector('div');
            let left = parseInt(window.getComputedStyle(div).left);
            div.style.left = left + 10 + 'px';
            if(left >= 200){
                clearInterval(timeId);
                interval(timeId => {
                    let width = parseInt(window.getComputedStyle(div).width);
                    div.style.width = width - 10 + 'px';
                    if(width <= 20){
                        clearInterval(timeId);
                    }
                })
            }
        });

        console.log("后盾人");
    </script>
</body>
</html>